//
//  SYCycleImages.m
//  06-轮播图
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SYCycleImages.h"
@interface  SYCycleImages ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *cycleScrollView;
@property (nonatomic,strong) UIPageControl *imagesPage;
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation SYCycleImages
#pragma mark - scrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
//    _imagesPage.currentPage = page - 1;
    if (page == 0) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
        
//        _imagesPage.currentPage = _imagesArray.count - 1;
    } else if (page == (_imagesArray.count - 1))
    {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
//        _imagesPage.currentPage = 0;
    }
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil) {
//        子控件初始化时frame不要与初始化方法的frame一样
//        推荐使用self.bounds / CGRectMake(0, 0, frame.size.width, frame.size.height)
//        _cycleScrollView.backgroundColor = [UIColor greenColor];
//        self.backgroundColor = [UIColor yellowColor];
        [self initCycleScrollView:frame];
        [self initImagePage:frame];
    }
    return self;
}

- (void)initCycleScrollView:(CGRect)frame
{
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.pagingEnabled = YES;
    _cycleScrollView.bounces = NO;
    _cycleScrollView.delegate = self;
    _cycleScrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:_cycleScrollView];
}



//- (void)dealloc
//{
//    [_timer dealloc];
//}





//后期需要添加page的位置不变，这里默认位置
- (void)initImagePage:(CGRect)frame
{
    
    _imagesPage = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _imagesPage.numberOfPages = 5;
    _imagesPage.center = CGPointMake(frame.size.width / 2, frame.size.height - 10);
    _imagesPage.backgroundColor = [UIColor blackColor];
    [self addSubview:_imagesPage];
    [_imagesPage addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)imagePageChange:(UIPageControl *)pageControl
{
    [_cycleScrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
//    [self scrollView:_cycleScrollView];
    
    
    
    
}

//- (void)scrollView:(UIScrollView *)scrollView
//{
//    NSInteger page1 = scrollView.contentOffset.x / self.bounds.size.width;
//    //    _imagesPage.currentPage = page - 1;
//    if (page1 == 0) {
//        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (_imagesArray.count - 2), 0);
//        
//        //        _imagesPage.currentPage = _imagesArray.count - 1;
//    } else if (page1 == (_imagesArray.count - 1))
//    {
//        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
//}
#pragma mark - ImageArray
- (void)setImagesWithArray:(NSArray *)array
{
    
    NSArray *imageArray = [self handleArray:array];
    self.imagesArray = imageArray;
    
    
    
}


- (NSArray *)handleArray:(NSArray *)array
{
    //    判断类型（不知道别人使用你的类的时候传进来的类型是什么）
    id firstObject = [array firstObject];
    //    判断对象是不是某种类型 isKindOfClass
    //    最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    if ([firstObject isKindOfClass:[NSString class]]) {
        //        把 字符串 转成 图片 数组
        //        使用数组创建、
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
        
    } else if([firstObject isKindOfClass:[UIImage class]]){
        //        直接使用数组创建ImageView
        [imagesArray setArray:array];
    }
    
    
    
    UIImage *firstImage = [imagesArray firstObject];
    UIImage *lastImage = [imagesArray lastObject];
    [imagesArray insertObject:lastImage atIndex:0];
    [imagesArray addObject:firstImage];
    //    逻辑判断是需要用到数组内容
    //    self.imagesArray = [NSArray arrayWithArray:array];
    
    [self createAllImageViewsWithImageArray:imagesArray];
    _cycleScrollView.contentSize = CGSizeMake(imagesArray.count * self.bounds.size.width, 0);
//    _imagesPage.numberOfPages = imagesArray.count;
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
//    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changePic) userInfo:nil repeats:YES];
    
    for (int i = 0; i < imagesArray.count; i++) {
//        NSLog(@"%ld",imagesArray.count);
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
    return [imagesArray copy];//把可变数组变为不可变
}

- (void)createAllImageViewsWithImageArray:(NSArray *)imageArray{
    
    
    
    
}

@end
